package com.kool.kmetrics.metrics.prometheus;

import com.alibaba.fastjson.JSON;
import com.kool.kmetrics.consumer.vo.ReceiveLog;
import com.kool.kmetrics.metrics.MetricsComputer;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 用户的各种报文类型的入站报文数、成功数、失败数、平均耗时、最大耗时；
 * 用户的主题的入站PUBLISH报文数；
 * 客户端的各种报文类型的入站报文数、成功数、失败数、平均耗时、最大耗时；
 * 客户端的主题的入站PUBLISH报文数；
 * 客户端地址的各种报文类型的入站报文数、成功数、失败数、平均耗时、最大耗时；
 * 客户端地址的主题的入站PUBLISH报文数；
 *
 * @author luyu
 */
@Component
public class ReceiveProm implements MetricsComputer {
    @Resource
    private Counter userReceiveCnt;

    @Bean
    public Counter userReceiveCnt() {
        return Counter.build("user_receive_cnt", "user receive counter")
                .labelNames("userName", "packetType", "success")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter userReceiveCost;

    @Bean
    public Counter userReceiveCost() {
        return Counter.build("user_receive_cost", "user receive cost")
                .labelNames("userName", "packetType", "cost")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter userTopicReceiveCnt;

    @Bean
    public Counter userTopicReceiveCnt() {
        return Counter.build("user_topic_receive_cnt", "user topic receive counter")
                .labelNames("userName", "topicName")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter clientReceiveCnt;

    @Bean
    public Counter clientReceiveCnt() {
        return Counter.build("client_receive_cnt", "client receive counter")
                .labelNames("clientId", "packetType", "success")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter clientReceiveCost;

    @Bean
    public Counter clientReceiveCost() {
        return Counter.build("client_receive_cost", "client receive cost")
                .labelNames("clientId", "packetType", "cost")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter clientTopicReceiveCnt;

    @Bean
    public Counter clientTopicReceiveCnt() {
        return Counter.build("client_topic_receive_cnt", "client topic receive counter")
                .labelNames("clientId", "topicName")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter remoteAddressReceiveCnt;

    @Bean
    public Counter remoteAddressReceiveCnt() {
        return Counter.build("remote_address_receive_cnt", "remote address receive counter")
                .labelNames("remoteAddress", "packetType", "success")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter remoteAddressReceiveCost;

    @Bean
    public Counter remoteAddressReceiveCost() {
        return Counter.build("remote_address_receive_cost", "remote address receive cost")
                .labelNames("remoteAddress", "packetType", "cost")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter remoteAddressTopicReceiveCnt;

    @Bean
    public Counter remoteAddressTopicReceiveCnt() {
        return Counter.build("remote_address_topic_receive_cnt", "remote address topic receive counter")
                .labelNames("remoteAddress", "topicName")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Override
    public void compute(String log) {
        ReceiveLog receiveLog = JSON.parseObject(log, ReceiveLog.class);
        String packetType = receiveLog.getPacketType();
        String isSuccess = receiveLog.getIsSuccess();
        Long costTime = receiveLog.getCostTime();
        String userName = receiveLog.getUserName();
        String clientId = receiveLog.getClientId();
        String remoteAddress = receiveLog.getRemoteAddress();
        String topicName = receiveLog.getTopicName();

        userReceiveCnt.labels(userName, packetType, isSuccess).inc();
        userReceiveCost.labels(userName, packetType, costTime.toString()).inc();
        clientReceiveCnt.labels(clientId, packetType, isSuccess).inc();
        clientReceiveCost.labels(clientId, packetType, costTime.toString()).inc();
        remoteAddressReceiveCnt.labels(remoteAddress, packetType, isSuccess).inc();
        remoteAddressReceiveCost.labels(remoteAddress, packetType, costTime.toString()).inc();
        if ("1".equals(isSuccess)) {
            userTopicReceiveCnt.labels(userName, topicName).inc();
            clientTopicReceiveCnt.labels(clientId, topicName).inc();
            remoteAddressTopicReceiveCnt.labels(remoteAddress, topicName);
        }
    }
}
